package logic

import (
	"context"
	"fmt"
	"recruit/model"

	"recruit/recruitrpc/internal/svc"
	"recruit/recruitrpc/recruitrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type PositionAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPositionAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PositionAddLogic {
	return &PositionAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PositionAddLogic) PositionAdd(in *recruitrpc.PositionAddRequest) (*recruitrpc.PositionAddResponse, error) {
	// todo: add your logic here and delete this line
	var position model.Position
	model.DB.Where("name=?", in.Name).Find(&position)
	if position.ID != 0 {
		return nil, fmt.Errorf("岗位已发布")
	}
	position = model.Position{
		Name:  in.Name,
		Class: in.Class,
		Add:   in.Add,
	}
	model.DB.Delete(&position)
	return &recruitrpc.PositionAddResponse{
		PositionId: int64(position.ID),
	}, nil
}
